//
//  LCTaskListViewController.m
//  lechuang
//
//  Created by Mr_hi on 2023/3/31.
//  Copyright © 2023 Mooyi. All rights reserved.
//

#import "LCTaskListViewController.h"
#import "LCTaskService.h"
#import "LCTaskTableViewCell.h"

#import "XWXAllXieYiController.h"

#import "LCFirstTrialViewController.h"

@interface LCTaskListViewController ()<UITableViewDelegate, UITableViewDataSource>

@property (strong, nonatomic)LCUnionModel * unionModel;

@property (weak, nonatomic) IBOutlet UITableView *tableView;

@property (assign, nonatomic)NSInteger page;

@property (strong, nonatomic)NSMutableArray * dataArr;

@property (nonatomic, assign) BOOL canScroll;

@end

@implementation LCTaskListViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    [self configUI];
    [self getUnionData];
    [self setTableViewRefresh];
}

-(void)configUI {
    self.page = 1;
    self.canScroll = NO;
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(taskPageControllerGoTop:) name:LCTaskPageControllerGoTopNoti object:nil];
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(taskPageControllerLeaveTop:) name:LCTaskPageControllerLeaveTopNoti object:nil];
}

-(void)taskPageControllerGoTop:(NSNotification *)noti {
    self.canScroll = YES;
}

-(void)taskPageControllerLeaveTop:(NSNotification *)noti {
    self.canScroll = NO;
}

-(void)scrollViewDidScroll:(UIScrollView *)scrollView {
    if (self.canScroll) {
        CGFloat offsetY = scrollView.contentOffset.y;
        if (offsetY <= 0) {
            [self.tableView setContentOffset:CGPointZero];
            [[NSNotificationCenter defaultCenter] postNotificationName:LCTaskPageControllerLeaveTopNoti object:@(YES)];
        }
    } else {
        [self.tableView setContentOffset:CGPointZero];
    }
}

-(void)setTableViewRefresh {
    self.tableView.mj_header = [MJRefreshNormalHeader headerWithRefreshingBlock:^{
        self.page = 1;
        [self getTaskListDataWithPage:1];
    }];
    self.tableView.mj_header.automaticallyChangeAlpha = YES;
    
    self.tableView.mj_footer = [MJRefreshAutoNormalFooter footerWithRefreshingBlock:^{
        self.page++;
        [self getTaskListDataWithPage:self.page];
    }];
    self.tableView.mj_footer.automaticallyChangeAlpha = YES;
    
    self.tableView.ly_emptyView = [LCEmptyView emptyViewWithImageStr:nil titleStr:nil detailStr:@"暂无数据"];
}


-(void)getUnionData {
    [LCTaskService getUnionDataWithBlock:^(BOOL isSuccess, NSArray<LCUnionModel *> *list) {
        if (isSuccess) {
            for (LCUnionModel * unionM in list) {
                if ([unionM.unionmodel isEqualToString:@"闪游"]) {
                    self.unionModel = unionM;
                }
            }
            [self getTaskListDataWithPage:1];
        }
    }];
}

- (void)getTaskListDataWithPage:(NSInteger)page {
    if (self.orderType != -1) {
        [self getAreaTaskListWithPage:page];
    } else {
        [self getXunKaTaskListWithPage:page];
    }
}

- (void)getAreaTaskListWithPage:(NSInteger)page {
    [LCTaskService getAreaTaskListWithAppId:self.unionModel.config.iosappid Key:self.unionModel.config.iosappsecret OrderType:self.orderType PageNum:page Block:^(BOOL isSuccess, NSArray<LCTaskModel *> *list, NSInteger hasData) {
        [self.tableView.mj_header endRefreshing];
        [self.tableView.mj_footer endRefreshing];
        if (isSuccess) {
            if (page == 1) {
                [self.dataArr removeAllObjects];
            }
            [self.dataArr addObjectsFromArray:list];
            if (!hasData) {
                [self.tableView.mj_footer endRefreshingWithNoMoreData];
            }
            [self.tableView reloadData];
        } else {
            if (self.page != 1) {
                self.page--;
            }
        }
    }];
}

- (void)getXunKaTaskListWithPage:(NSInteger)page {
    [LCTaskService xunKaTaskListWithPageNum:page Block:^(BOOL isSuccess, NSArray<LCXunKaTaskModel *> *list, NSInteger total) {
        [self.tableView.mj_header endRefreshing];
        [self.tableView.mj_footer endRefreshing];
        if (isSuccess) {
            if (page == 1) {
                [self.dataArr removeAllObjects];
            }
            [self.dataArr addObjectsFromArray:list];
            if (self.dataArr.count >= total) {
                [self.tableView.mj_footer endRefreshingWithNoMoreData];
            }
            [self.tableView reloadData];
        } else {
            if (self.page != 1) {
                self.page--;
            }
        }
    }];
}

-(NSMutableArray *)dataArr {
    if (!_dataArr) {
        _dataArr = [NSMutableArray array];
    }
    return _dataArr;
}

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return self.dataArr.count;
}

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    return 92;
}

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    if (self.orderType != -1) {
        LCTaskTableViewCell * cell = [tableView dequeueReusableCellWithIdentifier:@"LCTaskTableViewCell"];
        cell.task = self.dataArr[indexPath.row];
        return cell;
    } else {
        LCXunKaTaskTableViewCell * cell = [tableView dequeueReusableCellWithIdentifier:@"LCXunKaTaskTableViewCell"];
        cell.task = self.dataArr[indexPath.row];
        return cell;
    }
}

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    if (self.orderType != -1) {
        LCTaskModel * task = self.dataArr[indexPath.row];
        NSString * channelId = self.unionModel.config.iosappid;
        NSString * userId = kFORMAT(@"%@",LCUserDefGet(Userid));
        NSString * sign = [LCFunction ns_md5:kFORMAT(@"%@-%@:sdkUserId=%@&",channelId, self.unionModel.config.iosappsecret,userId)].uppercaseString;
        
        NSLog(@"%@",AreaTaskDetailWeb(channelId, userId, sign, kFORMAT(@"%ld",task.taskId)));
        
        XWXAllXieYiController *webVC = [XWXAllXieYiController new];
        webVC.hidesBottomBarWhenPushed = YES;
        webVC.tileStr = task.taskTitle;
        webVC.urlStr = AreaTaskDetailWeb(channelId, userId, sign, kFORMAT(@"%ld",task.taskId));
        webVC.isPushFrom = YES;
        webVC.hidesBottomBarWhenPushed = YES;
        [self.parentVC.navigationController pushViewController:webVC animated:YES];
    } else {
        LCFirstTrialViewController * pushVC = [LCFirstTrialViewController xx_instantiateFromStoryboardNamed:@"Task"];
        pushVC.task = self.dataArr[indexPath.row];
        [self.parentVC.navigationController pushViewController:pushVC animated:YES];
    }
}

@end
